import Vue from 'vue'
import VueRouter from 'vue-router'
import nprogress from 'nprogress'//导入进度条插件
//导入进度条样式
import 'nprogress/nprogress.css'
//导入进度条样式
Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    meta: { title: 'index' },
    component: () => import('../views/Index.vue'),
    children: [
      {
        path: '/index/home',
        name: 'home',
        meta: { title: '首页' },
        component: () => import('../views/Index/Home.vue'),
      },
      {
        path: '/index/home1',
        name: 'home1',
        meta: { title: '爆爆团' },
        component: () => import('../views/Index/Home1.vue'),
      },
      {
        path: '/index/home2',
        name: 'home2',
        meta: { title: '订单' },
        component: () => import('../views/Index/Home2.vue'),
      },
      {
        path: '/index/home3',
        name: 'home3',
        meta: { title: '我的' },
        component: () => import('../views/Index/Home3.vue'),
      },
      // 二级路由重定向
      {
        path: '/index',
        redirect: '/index/home'
      },
      //二级404
      {
        path: '*',
        component: () => import('../views/Index/NotFound2.vue'),
      }
    ]
  },
  {
    path: '/registered',
    name: 'registered',
    meta: { title: '注册' },
    component: () => import('../views/Registered.vue'),
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录' },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/order',
    name: 'order',
    meta: { title: 'order' },
    component: () => import('../views/Order.vue'),
  },
  {
    path: '/detailed/:id',
    name: 'detailed',
    meta: { title: '详情' },
    component: () => import('../views/Detailed.vue'),
  },
  {
    path: '/search',
    name: 'search',
    meta: { title: '搜素' },
    component: () => import('../views/Search.vue'),
    children: [
      {
        path: '/search/sclist',
        name: 'SClist',
        meta: { title: '搜索列表' },
        component: () => import('../views/search/SearchList.vue')
      },
      {
        path: '/search/found',
        name: 'found',
        meta: { title: '发现' },
        component: () => import('../views/search/SearchBox.vue')
      },
      {
        path: '/search',
        redirect: '/search/found'
      }
    ]
  },
  {
    path: '/coll',
    name: 'coll',
    meta: { title: '收藏' },
    component: () => import('../views/Collection.vue'),
  },
  //一级路由定向
  {
    path: '/',
    redirect: '/index'
  },
  //404路由
  {
    path: '*',
    component: () => import('../views/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//设置实例方法( 导航守卫方法 )

// 路由的全局前置守卫方法
router.beforeEach((to, from, next) => {
  //参数一: to 新的路由对象
  //参数二: from 旧的路由对象
  //参数三: next 路由控制方法, 调用该方法则允许路由跳转, 未调用该方法则不允许路由跳转
  // console.log(to.meta)
  nprogress.start();//开始加载动画（进度条）
  document.title = to.meta.title;
  next();
  var token = localStorage.getItem('token');

  if (to.path == '/registered') {
    next();
  } else {
    if (to.path == '/login') {
      if (token) {
        next('/');
      } else {
        next();
      }
    } else {
      if (token) {
        next();
      } else {
        next('/login');
      }
    }
  }
});

router.afterEach((to, from) => {
  nprogress.done();//停止播放
})


export default router
